<?php

/**
 * Role storage class
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Roles.php 38 2009-02-18 17:12:39Z monzee $
 */
class Admin_Array_Roles
{
    protected $_roles = array();

    public function __construct()
    {
        $this->_roles = array(
            array(
                'id'          => 'admin',
                'description' => 'Administrator',
            ),
            array(
                'id'          => 'guest',
                'description' => 'Guest',
            ),
            array(
                'id'          => 'member',
                'description' => 'Member',
            ),
            array(
                'id'          => 'author',
                'description' => 'Author',
            ),
            array(
                'id'          => 'mod',
                'description' => 'Moderator',
            ),
            array(
                'id'          => 'editor',
                'description' => 'Editor',
            ),
        );
    }

    /**
     * Stores and returns the role with the specified id
     *
     * @param mixed $id
     * @return array
     */
    public function getRoleById($roleId)
    {
        foreach ($this->_roles as $role) {
            if ($role['id'] == $roleId) {
                $this->_role = $role;
                return $role;
            }
        }
        return null; // or throw an exception?
    }

    /**
     * Returns an array of ancestors of currently stored role
     *
     * @param mixed $roleId
     * @return array
     */
    public function getParentRoles($roleId)
    {
        $heirarchy = new Admin_Array_RoleInheritance();
        $parents = $heirarchy->getParents($roleId);

        $ret = array();
        foreach ($parents as $parentId) {
            $ret[] = $this->getRoleById($parentId);
        }

        return $ret;
    }

    public function getAllRoles()
    {
        return $this->_roles;
    }

}
